﻿using ECLogger;
using Panuon.UI.Silver;
using System;
using System.Windows;

namespace FESSC_WORKBENCH
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class WORKBENCH : WindowX
    {
        private Panuon.UI.Silver.Core.IPendingHandler penbox;
        private bool isLoggedOutFinished = false;

        public WORKBENCH()
        {
            InitializeComponent();
            Init();
        }

        private void Init()
        {
            try
            {
                string logfile = Environment.CurrentDirectory + "\\workbench.log";
                AppVar.log.setLogLevel(Logger.LOG_LEVEL.DEBUG);
                AppVar.log.Init(logfile, TB_LOG);
                AppVar.log.Info("*** FESSC WORKBENCH CLIENT ***");
            }
            catch (Exception ex)
            {
                AppVar.log.Error(ex.ToString());
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Func<ECMessageQueue.MessageQueue.MQMESSAGE, bool> userLogout =
                delegate (ECMessageQueue.MessageQueue.MQMESSAGE mqm)
                {
                    bool ret = true;
                    //while (penbox == null) System.Threading.Thread.Sleep(100);
                    //penbox.UpdateMessage("Server response\r\n" + mqm.content);
                    //System.Threading.Thread.Sleep(2000);
                    //if (penbox != null) penbox.Close();
                    isLoggedOutFinished = true;
                    return ret;
                };
            AppVar.opDict.reg("USERLOGOUT", userLogout);

            if (AppVar.uid != string.Empty)
            {
                async void wait()
                {
                    while (!isLoggedOutFinished)
                    {
                        await System.Threading.Tasks.Task.Delay(100);
                    }
                    await System.Threading.Tasks.Task.Delay(1000);
                    penbox.UpdateMessage("已注销");
                    await System.Threading.Tasks.Task.Delay(2000);
                    penbox.Close();
                }
                OP_USERLOGIN.doLogout();
                penbox = PendingBox.Show("等待服务器响应...", "正在注销", false, AppVar.mainwnd);
                wait();
            }

            if (AppVar.mqworker != null) AppVar.mqworker.close();

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            AppVar.loginDialog = new LoginDialog();
            if (!(bool)(AppVar.loginDialog.ShowDialog()))
            {
                Window_Closing(this, null);
                Application.Current.Shutdown();
            }
            LB_TITLE_USER.Content = string.Format("{0} 您好，欢迎使用 FESSC WORKBENCH", AppVar.userinfo.name);
            TVI_WEEKREPORT.IsSelected = true;
            TVI_WEEKREPORT_Selected(this, null);
        }

        private void TVI_WEEKREPORT_Selected(object sender, RoutedEventArgs e)
        {
            PAGEFRAME.Navigate(new Uri("WeekReport.xaml", UriKind.Relative));
        }
    }
}
